package com.ggy.babybed.activity;

import android.app.Application;
import android.content.SharedPreferences;
import android.util.Log;


import com.ggy.babybed.R;
import com.ggy.babybed.util.Utility;

import java.util.HashMap;
import java.util.List;

/**
 * Created by sizi on 2016/5/19.
 * TODO 非静态方法时，，getInstance().func()操作，声明初始化的变量会为空？？？？
 */
public class MyApplication extends Application{
    private static String TAG = "MyApplication";

    private static SharedPreferences sp;
    private HashMap<String,List<Object>> mLastDataMap=new HashMap<>();

    /** 头像保存路径 */
    private static String headpicSavePath;
    /** 图像缓存路径 */
    private static String picCache;
    /** 视频截图保存路径 */
    private static String videoShotDirPath;
    /** 视频缓存路径 */
    private static String videoCacheDirPath;

    @Override
    public void onCreate() {
        super.onCreate();
        sp = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
        headpicSavePath = Utility.createFolder(getApplicationContext(), getString(R.string.headpic_save_path));
        picCache = Utility.createFolder(getApplicationContext(), getString(R.string.pic_cache));
        videoShotDirPath = Utility.createFolder(getApplicationContext(), getString(R.string.video_shot_dir));
        videoCacheDirPath = Utility.createFolder(getApplicationContext(), getString(R.string.video_cache_dir));
    }

    public List<Object> getLastNodeData(String node){
        if(!mLastDataMap.containsKey(node)){
            return null;
        }
        return mLastDataMap.get(node);
    }

    public boolean setLastNodeData(String node,List<Object> data){
        mLastDataMap.put(node,data);
        return true;
    }

    public static void setValue(String key, String value){
        if(null == key || null == value){
            return;
        }
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
        Log.i(TAG, "save success");
    }

    public static String getValue(String key){
        Log.i(TAG, sp.getString(key, ""));
        return sp.getString(key, "");
    }

    public static SharedPreferences getPreferences(){
        return sp;
    }

    public static String getHeadpicSavePath() {
        return headpicSavePath;
    }

    public static String getPicCache() {
        return picCache;
    }

    public static String getVideoShotDirPath() {
        return videoShotDirPath;
    }

    public static String getVideoCacheDirPath() {
        return videoCacheDirPath;
    }

}
